package Test;

import ZJ.ColumnName;
import ZJ.TabName;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseDao<T> {

    public int add(T t) {
        //根据对象获取class对象
        Class<?> aClass = t.getClass();
        //获取表名  类的注解
        TabName declaredAnnotation = aClass.getDeclaredAnnotation(TabName.class);
        //获取注解的属性
        String tabName = declaredAnnotation.name();
        System.out.println("表名 = " + tabName);
        //获取@ColumnName里面的属性值
        Field[] declaredFields = aClass.getDeclaredFields();
        List<String> columnNames = new ArrayList<>();//把所有的列名，放到一个集合里
        List columnValues = new ArrayList<>();//把所有列对应的值放到一个集合里
        for (Field declaredField : declaredFields) {
            //获取属性上面的注解
            ColumnName columnName = declaredField.getDeclaredAnnotation(ColumnName.class);
//            System.out.println("columnName = " + columnName);
//            columnName = @ZJ.ColumnName(name=id)
//            System.out.println("---------------------");
            String name = columnName.name();
//            System.out.println("列名 = " + name);
            columnNames.add(name);
            //获取列的值
            declaredField.setAccessible(true);
            try {
                Object o = declaredField.get(t);
                System.out.println("列的名字是：" + name + "对应的值是：" + o);
                columnValues.add("'" + o + "'");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        String columname = columnNames.toString().replace("[", "(").replace("]", ")");
        String columnval = columnValues.toString().replace("[", "(").replace("]", ")");
        String sql = "insert into " + tabName + columname + "values" + columnval;
        System.out.println("sql = " + sql);

        int result = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true", "root", "123456");
            System.out.println(con);
            PreparedStatement ps = con.prepareStatement(sql);
            result = ps.executeUpdate();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public int del(T t) {
        //根据对象获取class对象
        Class<?> aClass = t.getClass();
        //获取表名
        // 1.类的注解属性————>该类对应的注解对象
        TabName tabName = aClass.getDeclaredAnnotation(TabName.class);
        //2.根据注解属性的值获取表名
        String name = tabName.name();
        System.out.println("name = " + name);
        //获取列名 Field[] getDeclaredFields()
        //1.获取类对象里的所有属性
        Field[] declaredFields = aClass.getDeclaredFields();
        List<Object> list = new ArrayList<>();
        List<Object> list1 = new ArrayList<>();
        for (Field declaredField : declaredFields) {
            //2，获取类属性注解
            ColumnName declaredAnnotation = declaredField.getDeclaredAnnotation(ColumnName.class);
//            System.out.println("列 = " + declaredAnnotation);

            //获取列名，根据类属性对应注解的值获取列名
            String name1 = declaredAnnotation.name();
            list.add(name1);
            System.out.println("name1 = " + name1);
            declaredField.setAccessible(true);
//获取列的值
            try {
                Object o = declaredField.get(t);
                list1.add(o);
                System.out.println("o = " + o);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //delct from biao where id=?

        }
        String sql = "DELETE from " + name + " where " + list.get(0) + "=" + list1.get(0);
        System.out.println("sql = " + sql);

        int result = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true", "root", "123456");
            System.out.println(con);
            PreparedStatement ps = con.prepareStatement(sql);
            result = ps.executeUpdate();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public int upd(T t) {
        //根据类对象获取Class对象
        Class<?> aClass = t.getClass();
        //获取表名
        // 1.类的注解属性————>该类对应的注解对象
        TabName tabName = aClass.getDeclaredAnnotation(TabName.class);
        String name = tabName.name();
        System.out.println("表名 = " + name);
        Field[] declaredFields = aClass.getDeclaredFields();
        //列
        List<Object> lie = new ArrayList<>();
        //值
        List<Object> zhi = new ArrayList<>();


        for (Field declaredField : declaredFields) {
            //2，获取类属性注解
//            ColumnName declaredAnnotation = declaredField.getDeclaredAnnotation(ColumnName.class);

            //获取列名，根据类属性对应注解的值获取列名
            String name1 = declaredField.getName();

            lie.add(name1);
            System.out.println("对应的列名 = " + name1);

            declaredField.setAccessible(true);
            //获取列的值
            try {
                Object o = declaredField.get(t);
                zhi.add(o);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
//UPDATE 表 set 列='值' where id=33
        String sql = "UPDATE " + name + " set " + lie.get(1) + "=" + "'" + zhi.get(1) + "'" + " where " + lie.get(0) + " = " + zhi.get(0);
        System.out.println("sql = " + sql);

        int result = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true", "root", "123456");
            System.out.println(con);
            PreparedStatement ps = con.prepareStatement(sql);
            result = ps.executeUpdate();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public List<Map<Object,Object>> sel(T t) {
        List<Map<Object,Object>> arrayList = new ArrayList<>();
        //获取类的class对象
        Class<?> aClass = t.getClass();
        //获取表名，根据类的注解属性————>该类对应的注解对象
        TabName tabName = aClass.getDeclaredAnnotation(TabName.class);
        String name = tabName.name();//表名
//        System.out.println("name = " + name);
/*        Field[] declaredFields = aClass.getDeclaredFields();
        List<Object> lie = new ArrayList<>();
        List<Object> zhi = new ArrayList<>();

        for (Field declaredField : declaredFields) {
            //对应的列名
            String name1 = declaredField.getName();
            lie.add(name1);
//            System.out.println("name1 = " + name1);
            declaredField.setAccessible(true);
            try {
                Object o = declaredField.get(t);
                zhi.add(o);
//                System.out.println("o = " + o);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }*/
//select * from 表 where id=?;
        String sql = "select " + " * " + " from " + name ;/*+ " where " + lie.get(0) + " = " + zhi.get(0)*/;
        System.out.println("sql = " + sql);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true", "root", "123456");
//            System.out.println(con);
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            //获取列的属性和类型的信息
            ResultSetMetaData metaData = rs.getMetaData();
            //获取列的总数
            int columnCount = metaData.getColumnCount();
            while (rs.next()) {
                Map<Object, Object> map = new HashMap<>();
                //获取列值
                for (int i = 0; i < columnCount; i++) {
                    //根据列的索引获取列名
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    //列值
                    Object columnValue = rs.getObject(i + 1);
                    //添加到map里
                    map.put(columnLabel, columnValue);
                }
                arrayList.add(map);

            }
            System.out.println("id     classsname      blance");
            for (Map<Object, Object> map1 : arrayList) {
                System.out.println(map1.get("id")+"      "+map1.get("classname")+"        "+map1.get("blance")
                );

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arrayList;
    }


}
